import {handleRelation} from '../../../../utils/tool'
var app = getApp()
Page({
  data: {
    currentPage: '',
    editable:false,
    isAdmin: false,
    child: null,
    record: null,
    form: {
      childId: ''
    },
    options: null
  },
  onLoad(options){
    wx.setNavigationBarTitle({
      title: options.childName
    })
    var form = this.data.form
    form.childId = options.childId
    this.setData({
      currentPage: options.currentPage,
      editable: options.currentPage=='mine',
      options,
      form,
      isAdmin: app.currentChild.isAdmin
    })
    this.getBabyDetail()
  },
  toWebview(){
    wx.navigateTo({
      url: app.xhx_route.webview,
    })
  },
  getBabyDetail(){
    var that = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['getBabyDetail'],
      data: this.data.form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        that.handleBabyDetail(res.data)
      }
    })
  },
  // 处理宝贝详情
  handleBabyDetail(detail){
    detail.child = handleRelation(detail.child)
    detail.record.kilocalorie = (detail.record.calorie/1000).toFixed(2)
    
    detail.record.trainingTime = parseInt(detail.record.trainingTime/60)
    this.setData({
      child: detail.child,
      record: detail.record
    })
  },
  // 取消关注
  deleteFans(){
    var _this = this

    wx.showModal({
      title: '提示',
      content: `您确定取消关心${this.data.options.childName}吗`,
      success (res) {
        if (res.confirm) {
          // 删除
          var form = {
            childUserId: _this.data.options.childId
          }
          app.middleware.requestAjax({
            serAddr: app.serAddrs['delFans'],
            data: form,
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success() {
              wx.showToast({
                title: '取消关心成功',
                success(){
                  _this.updatePrevPageData(form)
                  setTimeout(()=>{
                    wx.navigateBack()
                  },1000)
                }
              })
            }
          })
        }
      }
    })
  },
  // 删除宝贝
  deleteBaby(){
    var _this = this

    wx.showModal({
      title: '提示',
      content: `您确定删除宝贝${this.data.options.childName}吗`,
      success (res) {
        if (res.confirm) {
          // 删除
          var form = {
            childUserId: _this.data.options.childId
          }
          app.middleware.requestAjax({
            serAddr: app.serAddrs['delBaby'],
            data: form,
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success() {
              wx.showToast({
                title: '宝贝删除成功',
                success(){
                  _this.updatePrevPageData(form)
                  setTimeout(()=>{
                    wx.navigateBack()
                  },1000)
                }
              })
            }
          })
        }
      }
    })
  },
  // 更新宝贝列表
  updatePrevPageData(form){
    var pageData = getCurrentPages()
    var pageLength = pageData.length
    // 宝贝列表
    var prevPageData = pageData[pageLength-2]

    // 判断是通过宝贝列表进入的详情还是关心列表
    var list = prevPageData.data.currentPage === 'mine'?prevPageData.data.babyList:prevPageData.data.carefulUserList
    console.log(list)
    var babyListLen = list.length
    var newList
    for (var i =0;i<babyListLen;i++) {
      var item = list[i]
      console.log(item.childId,this.data.form.childId)
      if (item.childId==this.data.form.childId) {
        list.splice(i,1)
        break
      }
    }
    var listKey = prevPageData.data.currentPage === 'mine'?'babyList':'carefulUserList'
    prevPageData.setData({
      [listKey]: list
    })
  },
  // 跳转宝贝二维码
  showQrcode(){
    var child = this.data.child
    var gender = child.sex==0?'male':'female'
    wx.navigateTo({
      url: app.xhx_route.babyQrcode+`?childId=${this.data.form.childId}&gender=${gender}&childName=${child.nickname}&avator=${child.picture}`
    })
  },
  editBabyDetail(){
    wx.navigateTo({
      url: app.xhx_route.babyDetailEdit + `?child=${JSON.stringify(this.data.child)}`
    })
  },
  navigateToGuardian(){
    wx.navigateTo({
      url: app.xhx_route.babyGuardian+'?childUserId='+this.data.options.childId
    })
  }
})
